package com.example.thornchg.yc_plane_game.enemy;

import com.example.thornchg.yc_plane_game.R;

import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.instant.CCHide;
import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.actions.interval.CCMoveTo;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCAnimation;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrame;
import org.cocos2d.sound.SoundEngine;

import java.util.ArrayList;
import java.util.Random;

public class Enemy_3 extends Enemy{
    private Random random;

    public Enemy_3() {
        super("enemy/enemy3_n1.png",2000,6,
                "enemy/enemy3_down%d.png");
        setScore(1000);

        random=new Random();
        setStartPosition();

        showRunAnimation();
        move();
    }
    public void showRunAnimation(){
        ArrayList<CCSpriteFrame> ccSpriteFrames=new ArrayList<>();
        ccSpriteFrames.add(CCSprite.spriteWithSpriteFrameName("enemy/enemy3_n1.png").displayedFrame());
        ccSpriteFrames.add(CCSprite.spriteWithSpriteFrameName("enemy/enemy3_n2.png").displayedFrame());
        CCAnimation ccAnimation=CCAnimation.animationWithFrames(ccSpriteFrames,.1f);
        CCAnimate ccAnimate=CCAnimate.action(ccAnimation,false);
        CCRepeatForever ccRepeatForever=CCRepeatForever.action(ccAnimate);
        runAction(ccRepeatForever);
    }

    @Override
    public void resume() {
        showRunAnimation();
        super.resume();

    }

    @Override
    public void playDeadEffect() {
        SoundEngine.sharedEngine().playEffect(CCDirector.theApp, R.raw.enemy3_dead);
    }

    @Override
    public void move() {
        CCMoveTo ccMoveTo = CCMoveTo.action(8f, CCLayer.ccp(getPosition().x, -getBoundingBox().size.getHeight()));
        CCHide ccHide = CCHide.action();
        CCSequence ccSequence = CCSequence.actions(ccMoveTo, ccHide);
        runAction(ccSequence);
    }

    @Override
    public void setStartPosition() {
        float x = random.nextFloat() * (winSize.getWidth() - 180) + 90;
        setPosition(CCLayer.ccp(x, winSize.getHeight()));
    }

    @Override
    public void showBeAttacked() {
        ArrayList<CCSpriteFrame> ccSpriteFrames=new ArrayList<>();
        ccSpriteFrames.add(CCSprite.spriteWithSpriteFrameName("enemy/enemy3_hit.png").displayedFrame());
        ccSpriteFrames.add(CCSprite.spriteWithSpriteFrameName("enemy/enemy3_n1.png").displayedFrame());
        CCAnimation ccAnimation=CCAnimation.animationWithFrames(ccSpriteFrames,.01f);
        CCAnimate ccAnimate=CCAnimate.action(ccAnimation,false);
        runAction(ccAnimate);
    }
}
